#include "friendreqdialog.h"
#include "ui_friendreqdialog.h"
#include"usermgr.h"
#include"globalsignal.h"

#include <tcpmgr.h>

friendReqDialog::friendReqDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::friendReqDialog)
{
    ui->setupUi(this);
    ui->plainTextEdit->setFocus();
}

friendReqDialog::~friendReqDialog()
{
    qDebug("friendReqDialog::~friendReqDialog");
    delete ui;
}

void friendReqDialog::setUser(const QString &name, const QString& message)
{
    ui->label_2->setText(name);
     ui->label->setPixmap(QPixmap(AvatarPath.arg(name)));
    ui->plainTextEdit->setPlainText(message);
}

void friendReqDialog::setCheckStyle()
{
    ui->plainTextEdit->setReadOnly(true); // 设置为只读
    ui->label_3->setText(ui->label_2->text() + "申请添加你为好友");
    _isReq = false;
}

void friendReqDialog::on_pushButton_2_clicked()
{
    this->close();
}

void friendReqDialog::on_pushButton_clicked()
{
    if (_isReq){
        QJsonObject jsonObj;
        jsonObj["token"] = UserMgr::GetInstance()->getToken();
        jsonObj["message"] = ui->plainTextEdit->toPlainText();
        jsonObj["user"] = ui->label_2->text();
        QJsonDocument doc(jsonObj);
        emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_ADD_FRIEND_REQ, doc.toJson(QJsonDocument::Compact));
    }else{
        QJsonObject jsonObj;
        jsonObj["token"] = UserMgr::GetInstance()->getToken();
        jsonObj["message"] = ui->plainTextEdit->toPlainText();
        jsonObj["user"] = ui->label_2->text();
        QJsonDocument doc(jsonObj);
        emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_AUTH_FRIEND_REQ, doc.toJson(QJsonDocument::Compact));

        emit GlobalSignal::GetInstance()->sig_add_friend(ui->label_2->text());
        {
            QJsonObject jsonObj;
            jsonObj["token"] = UserMgr::GetInstance()->getToken();
            jsonObj["message"] = "你好我同意了你的好友申请，现在可以开始聊天了！";
            jsonObj["receiver"] = ui->label_2->text();
            QJsonDocument doc(jsonObj);
            emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_TEXT_CHAT_MSG, doc.toJson(QJsonDocument::Compact));
            qDebug("发送打招呼消息！！！");
        }
        qDebug("同意");
    }
    this->close();
}
